from django.contrib import admin
from .models import Comment


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('comment_id', 'article', 'user', 'content_preview', 'p_comment_id', 'good_count', 'top_type', 'post_time')
    list_filter = ('top_type', 'post_time', 'article__p_board')
    search_fields = ('content', 'user__nickname', 'article__title')
    ordering = ('-post_time',)
    readonly_fields = ('good_count', 'post_time', 'nick_name', 'reply_nick_name')
    list_display_links = ('comment_id', 'article', 'user', 'content_preview', 'p_comment_id', 'good_count', 'top_type', 'post_time')
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('article', 'user', 'content', 'img_path')
        }),
        ('Reply message', {
            'fields': ('p_comment_id', 'reply_user_id')
        }),
        ('Status Information', {
            'fields': ('top_type', 'good_count', 'post_time')
        }),
        ('Reply and Replied Users', {
            'fields': ('nick_name', 'reply_nick_name')
        }),
    )
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = 'Content Preview'